{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-03-30T14:53:40.088Z",
     "start_time": "2024-03-30T14:53:38.887914Z"
    }
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "from torch import nn\n",
    "from torch.nn import functional as F"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 5.5.1. 加载和保存张量"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "66c505f3a917ae5a"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "x = torch.arange(4)\n",
    "torch.save(x, 'x-file')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T14:53:44.379077Z",
     "start_time": "2024-03-30T14:53:44.362460Z"
    }
   },
   "id": "f923de6d6cda48d9",
   "execution_count": 2
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([0, 1, 2, 3])"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x2 = torch.load('x-file')\n",
    "x2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T14:54:19.322512Z",
     "start_time": "2024-03-30T14:54:19.259754Z"
    }
   },
   "id": "6b0763b02d5cfd",
   "execution_count": 3
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "(tensor([0, 1, 2, 3]), tensor([0., 0., 0., 0.]))"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = torch.zeros(4)\n",
    "torch.save([x, y],'x-files')\n",
    "x2, y2 = torch.load('x-files')\n",
    "(x2, y2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T14:54:42.024629Z",
     "start_time": "2024-03-30T14:54:42.004218Z"
    }
   },
   "id": "95a8c6ad7a000eb2",
   "execution_count": 4
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "{'x': tensor([0, 1, 2, 3]), 'y': tensor([0., 0., 0., 0.])}"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mydict = {'x': x, 'y': y}\n",
    "torch.save(mydict, 'mydict')\n",
    "mydict2 = torch.load('mydict')\n",
    "mydict2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T14:54:59.913836Z",
     "start_time": "2024-03-30T14:54:59.904227Z"
    }
   },
   "id": "997b036fdc611f6d",
   "execution_count": 5
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 5.5.2. 加载和保存模型参数"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "278e3fa27cbaf76a"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "class MLP(nn.Module):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self.hidden = nn.Linear(20, 256)\n",
    "        self.output = nn.Linear(256, 10)\n",
    "\n",
    "    def forward(self, x):\n",
    "        return self.output(F.relu(self.hidden(x)))\n",
    "\n",
    "net = MLP()\n",
    "X = torch.randn(size=(2, 20))\n",
    "Y = net(X)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T14:56:16.480509Z",
     "start_time": "2024-03-30T14:56:16.461973Z"
    }
   },
   "id": "678582ce82291f03",
   "execution_count": 6
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "torch.save(net.state_dict(), 'mlp.params')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T14:56:31.803324Z",
     "start_time": "2024-03-30T14:56:31.795751Z"
    }
   },
   "id": "59e4fca6208b7733",
   "execution_count": 7
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "MLP(\n  (hidden): Linear(in_features=20, out_features=256, bias=True)\n  (output): Linear(in_features=256, out_features=10, bias=True)\n)"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clone = MLP()\n",
    "clone.load_state_dict(torch.load('mlp.params'))\n",
    "clone.eval()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T14:57:05.374541Z",
     "start_time": "2024-03-30T14:57:05.324902Z"
    }
   },
   "id": "b441a7887ba3e942",
   "execution_count": 8
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "(tensor([[-0.2431,  0.1427,  0.0482, -0.0667,  0.2122,  0.2632,  0.1622, -0.0862,\n          -0.1611, -0.0076],\n         [-0.1796, -0.1936, -0.1593, -0.1834,  0.1920,  0.0726,  0.0332, -0.1410,\n           0.0848,  0.5652]], grad_fn=<AddmmBackward0>),\n tensor([[-0.2431,  0.1427,  0.0482, -0.0667,  0.2122,  0.2632,  0.1622, -0.0862,\n          -0.1611, -0.0076],\n         [-0.1796, -0.1936, -0.1593, -0.1834,  0.1920,  0.0726,  0.0332, -0.1410,\n           0.0848,  0.5652]], grad_fn=<AddmmBackward0>))"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Y_clone = clone(X)\n",
    "Y, Y_clone"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T14:57:50.893610Z",
     "start_time": "2024-03-30T14:57:50.883346Z"
    }
   },
   "id": "789419ebfdea8c7a",
   "execution_count": 9
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[True, True, True, True, True, True, True, True, True, True],\n        [True, True, True, True, True, True, True, True, True, True]])"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Y == Y_clone"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T15:05:36.782532Z",
     "start_time": "2024-03-30T15:05:36.775593Z"
    }
   },
   "id": "78e5d45600a8ca14",
   "execution_count": 14
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "device(type='cpu')"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.hidden.weight.device"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T15:06:48.234743Z",
     "start_time": "2024-03-30T15:06:48.227095Z"
    }
   },
   "id": "87c6fc59ce2d660c",
   "execution_count": 18
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "(torch.nn.parameter.Parameter, torch.Tensor)"
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(net.hidden.weight), type(net.hidden.weight.data)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T15:07:10.233081Z",
     "start_time": "2024-03-30T15:07:10.224497Z"
    }
   },
   "id": "f5c37203959bd944",
   "execution_count": 20
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "970895e33413eae8"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
